#coding:utf-8
import time

import tornado.web

import settings
from live import manager

if settings.STORAGE == "file":
    from models_file import Dm

class SendDmHandler(tornado.web.RequestHandler):
    def post(self, room):
        new_dm = {
            "text" : self.get_body_argument("text", ""),
            "sender" : self.get_body_argument("sender", "nobody"),
            "position" : self.get_body_argument("position", settings.DEFAULT_DM_POSITION),
            "font" : self.get_body_argument("font", settings.DEFAULT_DM_FONT),
            "color" : self.get_body_argument("color", settings.DEFAULT_DM_COLOR),
            "size" : self.get_body_argument("size", settings.DEFAULT_DM_SIZE),
            "time" : str(int(time.time()*1000))
        }
        if len(new_dm["text"]) == 0:
            raise tornado.web.HTTPError(400)

        dm = Dm.create(**new_dm)
        manager.broadcast_dm(room, dm)

        self.write({"status":0, "data":"", "description":"success"})